//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a sample for BCGControlBar Library Professional Edition
// Copyright (C) 1998-2016 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions 
// of the accompanying license agreement.
//*******************************************************************************
//
// ChartLogarithmicAxisView.cpp : implementation file
//

#include "stdafx.h"
#include "bcgpchartexample.h"
#include "ChartLogarithmicAxisView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChartLogarithmicAxisView

IMPLEMENT_DYNCREATE(CChartLogarithmicAxisView, CBCGPChartExampleView)

CChartLogarithmicAxisView::CChartLogarithmicAxisView()
	: CBCGPChartExampleView(CChartLogarithmicAxisView::IDD)
{
	//{{AFX_DATA_INIT(CChartLogarithmicAxisView)
	m_bLogScale = TRUE;
	m_nLogBase = 3;
	//}}AFX_DATA_INIT
}

CChartLogarithmicAxisView::~CChartLogarithmicAxisView()
{
}

void CChartLogarithmicAxisView::DoDataExchange(CDataExchange* pDX)
{
	CBCGPChartExampleView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChartLogarithmicAxisView)
	DDX_Control(pDX, IDC_LOG_BASE, m_wndLogBase);
	DDX_Control(pDX, IDC_CHART, m_wndChart);
	DDX_Check(pDX, IDC_LOG_SCALE, m_bLogScale);
	DDX_CBIndex(pDX, IDC_LOG_BASE, m_nLogBase);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChartLogarithmicAxisView, CBCGPChartExampleView)
	//{{AFX_MSG_MAP(CChartLogarithmicAxisView)
	ON_BN_CLICKED(IDC_LOG_SCALE, OnUpdateChart)
	ON_CBN_SELENDOK(IDC_LOG_BASE, OnUpdateChart)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChartLogarithmicAxisView diagnostics

#ifdef _DEBUG
void CChartLogarithmicAxisView::AssertValid() const
{
	CBCGPChartExampleView::AssertValid();
}

void CChartLogarithmicAxisView::Dump(CDumpContext& dc) const
{
	CBCGPChartExampleView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChartLogarithmicAxisView message handlers

void CChartLogarithmicAxisView::OnInitialUpdate() 
{
	CBCGPChartExampleView::OnInitialUpdate();
	
	if (m_bIsReady)
	{
		return;
	}

	m_bIsReady = TRUE;

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	pChart->SetLegendPosition(BCGPChartLayout::LP_NONE);

	CBCGPChartSeries* pSeries = pChart->CreateSeries(_T("Series"));
	pSeries->SetCurveType (BCGPChartFormatSeries::CCT_SPLINE);

	pSeries->AddDataPoint(0.01);
	pSeries->AddDataPoint(0.5);
	pSeries->AddDataPoint(0.7);
	pSeries->AddDataPoint(1.4);
	pSeries->AddDataPoint(20);
	pSeries->AddDataPoint(50);
	pSeries->AddDataPoint(80);
	pSeries->AddDataPoint(190);
	pSeries->AddDataPoint(500);
	pSeries->AddDataPoint(1000);

	pChart->ShowDataMarkers();

	SetDefaultLineWidth();
	OnUpdateChart();
}

void CChartLogarithmicAxisView::OnUpdateChart()
{
	UpdateData();

	m_wndLogBase.EnableWindow(m_bLogScale);

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	CBCGPChartAxis* pYAxis = pChart->GetChartAxis(BCGP_CHART_Y_PRIMARY_AXIS);
	ASSERT_VALID(pYAxis);

	CString strLogBase;
	m_wndLogBase.GetLBText(m_nLogBase, strLogBase);

	double dblLogBase = 2.;

#if _MSC_VER < 1400
	_stscanf(strLogBase, _T("%lf"), &dblLogBase);
#else
	_stscanf_s(strLogBase, _T("%lf"), &dblLogBase);
#endif

	pYAxis->SetLogScale(m_bLogScale, dblLogBase);

	pChart->SetDirty();
	pChart->Redraw();
}
